package com.jinsilu.freight.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.BusUtils;
import com.hjq.base.BaseDialog;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.jinsilu.freight.R;
import com.jinsilu.freight.aop.SingleClick;
import com.jinsilu.freight.common.MyVBActivity;
import com.jinsilu.freight.databinding.ActivityMyCertificationBinding;
import com.jinsilu.freight.http.model.HttpData;
import com.jinsilu.freight.http.model.HttpListData;
import com.jinsilu.freight.http.request.GetDrivingLicenseApi;
import com.jinsilu.freight.http.request.GetIdCardApi;
import com.jinsilu.freight.http.request.GetInsuranceApi;
import com.jinsilu.freight.http.request.GetLicensePlateColorApi;
import com.jinsilu.freight.http.request.GetMyCertificationStatusApi;
import com.jinsilu.freight.http.request.GetQualificationsApi;
import com.jinsilu.freight.http.request.GetRoadTransportApi;
import com.jinsilu.freight.http.request.GetVehicleLicenseApi;
import com.jinsilu.freight.http.request.SubmitCertificateApi;
import com.jinsilu.freight.http.response.DrivingLicenseBean;
import com.jinsilu.freight.http.response.IdCardBean;
import com.jinsilu.freight.http.response.InsuranceBean;
import com.jinsilu.freight.http.response.LicensePlateColor;
import com.jinsilu.freight.http.response.MyCertificationBean;
import com.jinsilu.freight.http.response.QualificationsBean;
import com.jinsilu.freight.http.response.RoadTransportBean;
import com.jinsilu.freight.http.response.VehicleLicenseBean;
import com.jinsilu.freight.ui.dialog.MenuDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的认证页面
 *
 * @author yangyang
 * @time 2020-11-11 14:15
 */
public class MyCertificationActivity extends MyVBActivity<ActivityMyCertificationBinding> {

    private int mCurrentSelectIndex; // 当前选择上传的图片条目索引

    private List<LicensePlateColor> licensePlateColors; // 车牌颜色列表
    private String licensePlateColor = ""; // 车牌颜色
    private String licensePlateColorCode = ""; // 车牌颜色编码

    private BaseDialog licensePlateColorDialog;

    @Override
    protected void initView() {

        setOnClickListener(binding.tvUploadIdCardStatus, binding.tvUploadDrivingLicenseStatus,
                binding.tvUploadQualificationCertificateStatus, binding.tvLicensePlateNumber, binding.tvUploadVehicleLicenseStatus,
                binding.tvUploadRoadTransportStatus, binding.tvUploadPolicyOfInsuranceStatus, binding.tvBtnSubmitCertificate);
    }

    @Override
    protected void initData() {
//        getIdCardStatus();
//        getDrivingLicenseStatus();
//        getQualificationStatus();
//        getVehicleLicenseStatus();
//        getRoadTransportStatus();
//        getInsuranceStatus();
        getLicensePlateColor();
        getMyCertificationStatus();
    }

    /**
     * 获取车牌颜色
     */
    private void getLicensePlateColor() {
        EasyHttp.get(this)
                .api(new GetLicensePlateColorApi())
                .request(new HttpCallback<HttpListData<LicensePlateColor>>(this) {

                    @Override
                    public void onSucceed(HttpListData<LicensePlateColor> data) {
                        licensePlateColors = data.getData();
                        List<String> plateColorList = new ArrayList<>();
                        for (LicensePlateColor plateColor :
                                licensePlateColors) {
//                            if ("Y".equals(plateColor.getIsDefault())) // 设置默认车牌颜色
//                                binding.tvLicensePlateNumber.setText(plateColor.getDictLabel());
                            plateColorList.add(plateColor.getDictLabel());
                        }
                        initBottomSelectDialog(plateColorList);
                    }
                });
    }

    private void initBottomSelectDialog(List<String> data) {
        // 底部选择框
        licensePlateColorDialog = new MenuDialog.Builder(this)
                // 设置 null 表示不显示取消按钮
                //.setCancel(getString(R.string.common_cancel))
                // 设置点击按钮后不关闭对话框
                //.setAutoDismiss(false)
                .setList(data)
                .setListener(new MenuDialog.OnListener<String>() {

                    @Override
                    public void onSelected(BaseDialog dialog, int position, String string) {
                        licensePlateColor = licensePlateColors.get(position).getDictLabel();
                        licensePlateColorCode = licensePlateColors.get(position).getDictValue();
                        binding.tvLicensePlateNumber.setText(string);
                    }

                    @Override
                    public void onCancel(BaseDialog dialog) {
                        if (dialog.isShowing()) dialog.dismiss();
                    }
                })
                .create();
    }

    /**
     * 获取我的信息验证状态
     */
    private void getMyCertificationStatus() {
        EasyHttp.get(this)
                .api(new GetMyCertificationStatusApi())
                .request(new HttpCallback<HttpData<MyCertificationBean>>(this) {

                    @Override
                    public void onSucceed(HttpData<MyCertificationBean> data) {
                        MyCertificationBean myCertificationBean = data.getData();
                        if (1 == myCertificationBean.getCertificateStatus() || -1 == myCertificationBean.getCertificateStatus()) {
                            binding.tvBtnSubmitCertificate.setEnabled(false);
                        }
//                        if (myCertificationBean.getVehicleCertificationResult().getLicensePlateColor().equals("黄牌")) {
//                            binding.rbVehicleColorYellow.setChecked(true);
//                        } else if (myCertificationBean.getVehicleCertificationResult().getLicensePlateColor().equals("蓝牌")) {
//                            binding.rbVehicleColorBlue.setChecked(true);
//                        }
                        licensePlateColor = myCertificationBean.getVehicleCertificationResult().getLicensePlateColor();
                        licensePlateColorCode = myCertificationBean.getVehicleCertificationResult().getLicensePlateColorCode();
                        binding.tvLicensePlateNumber.setText(TextUtils.isEmpty(licensePlateColor) ? "" : licensePlateColor);

                        setCertificationStatusTips(myCertificationBean.getCertificateStatus());
                        setCertificateStatus(myCertificationBean.getDriverCertificationResult().getIdCardCertificationResult().getIdCardCertificationStatus(), binding.ivUploadIdCardStatus, binding.tvUploadIdCardStatus);
                        setCertificateStatus(myCertificationBean.getDriverCertificationResult().getDriversLicenseCertificationResult().getDriversLicenseCertificationStatus(), binding.ivUploadDrivingLicenseStatus, binding.tvUploadDrivingLicenseStatus);
                        setCertificateStatus(myCertificationBean.getDriverCertificationResult().getProfessionalQualificationCertificateResult().getProfessionalQualificationCertificateStatus(), binding.ivUploadQualificationCertificateStatus, binding.tvUploadQualificationCertificateStatus);
                        setCertificateStatus(myCertificationBean.getVehicleCertificationResult().getVehicleLicenseCertificationResult().getVehicleLicenseCertificationStatus(), binding.ivUploadVehicleLicenseStatus, binding.tvUploadVehicleLicenseStatus);
                        setCertificateStatus(myCertificationBean.getVehicleCertificationResult().getVehicleTransportLicenseCertificationResult().getVehicleTransportLicenseStatus(), binding.ivUploadRoadTransportStatus, binding.tvUploadRoadTransportStatus);
                        setCertificateStatus(myCertificationBean.getVehicleCertificationResult().getInsuranceCertificationResult().getInsuranceCertificationStatus(), binding.ivUploadPolicyOfInsuranceStatus, binding.tvUploadPolicyOfInsuranceStatus);
                    }
                });
    }

    private void setCertificationStatusTips(int status) {
        binding.tvNotificationTips.setVisibility(View.VISIBLE);
        switch (status) {
            case -2: // 未认证
                binding.tvNotificationTips.setText("未认证");
                binding.tvNotificationTips.setTextColor(Color.parseColor("#999999"));
                break;
            case 0: // 认证失败
                binding.tvNotificationTips.setText("认证失败");
                binding.tvNotificationTips.setTextColor(Color.parseColor("#E02020"));
                break;
            case 1: // 认证成功
                binding.tvNotificationTips.setText("认证成功");
                binding.tvNotificationTips.setTextColor(Color.parseColor("#3265F9"));
                break;
            case -1: // 提交全部认证资料 认证中
                binding.tvNotificationTips.setText("认证中");
                binding.tvNotificationTips.setTextColor(Color.parseColor("#ff9900"));
                break;
        }
    }

    /**
     * 获取身份验证状态
     */
    private void getIdCardStatus() {
        EasyHttp.get(this)
                .api(new GetIdCardApi())
                .request(new HttpCallback<HttpData<IdCardBean>>(this) {

                    @Override
                    public void onSucceed(HttpData<IdCardBean> data) {
                        IdCardBean idCardBean = data.getData();
                        setCertificateStatus(idCardBean.getIdCardCertificationStatus(), binding.ivUploadIdCardStatus, binding.tvUploadIdCardStatus);
                    }
                });
    }

    /**
     * 获取驾驶证状态
     */
    private void getDrivingLicenseStatus() {
        EasyHttp.get(this)
                .api(new GetDrivingLicenseApi())
                .request(new HttpCallback<HttpData<DrivingLicenseBean>>(this) {

                    @Override
                    public void onSucceed(HttpData<DrivingLicenseBean> data) {
                        DrivingLicenseBean drivingLicenseBean = data.getData();
                        setCertificateStatus(drivingLicenseBean.getDriversLicenseCertificationStatus(), binding.ivUploadDrivingLicenseStatus, binding.tvUploadDrivingLicenseStatus);
                    }
                });
    }

    /**
     * 获取从业资格证状态
     */
    private void getQualificationStatus() {
        EasyHttp.get(this)
                .api(new GetQualificationsApi())
                .request(new HttpCallback<HttpData<QualificationsBean>>(this) {

                    @Override
                    public void onSucceed(HttpData<QualificationsBean> data) {
                        QualificationsBean qualificationsBean = data.getData();
                        setCertificateStatus(qualificationsBean.getProfessionalQualificationCertificateStatus(), binding.ivUploadQualificationCertificateStatus, binding.tvUploadQualificationCertificateStatus);
                    }
                });
    }

    /**
     * 获取行驶证状态
     */
    private void getVehicleLicenseStatus() {
        EasyHttp.get(this)
                .api(new GetVehicleLicenseApi())
                .request(new HttpCallback<HttpData<VehicleLicenseBean>>(this) {

                    @Override
                    public void onSucceed(HttpData<VehicleLicenseBean> data) {
                        VehicleLicenseBean vehicleLicenseBean = data.getData();
                        setCertificateStatus(vehicleLicenseBean.getVehicleLicenseCertificationStatus(), binding.ivUploadVehicleLicenseStatus, binding.tvUploadVehicleLicenseStatus);
                    }
                });
    }

    /**
     * 获取道路运输证状态
     */
    private void getRoadTransportStatus() {
        EasyHttp.get(this)
                .api(new GetRoadTransportApi())
                .request(new HttpCallback<HttpData<RoadTransportBean>>(this) {

                    @Override
                    public void onSucceed(HttpData<RoadTransportBean> data) {
                        RoadTransportBean roadTransportBean = data.getData();
                        setCertificateStatus(roadTransportBean.getVehicleTransportLicenseStatus(), binding.ivUploadRoadTransportStatus, binding.tvUploadRoadTransportStatus);
                    }
                });
    }

    /**
     * 获取保险单状态
     */
    private void getInsuranceStatus() {
        EasyHttp.get(this)
                .api(new GetInsuranceApi())
                .request(new HttpCallback<HttpData<InsuranceBean>>(this) {

                    @Override
                    public void onSucceed(HttpData<InsuranceBean> data) {
                        InsuranceBean insuranceBean = data.getData();
                        setCertificateStatus(insuranceBean.getInsuranceCertificationStatus(), binding.ivUploadPolicyOfInsuranceStatus, binding.tvUploadPolicyOfInsuranceStatus);
                    }
                });
    }

    private void setCertificateStatus(int certificateStatus, ImageView imageView, TextView textView) {
        switch (certificateStatus) {
            case -2: // 未认证
                imageView.setImageResource(R.drawable.upload_success_ic);
                imageView.setColorFilter(Color.parseColor("#999999"));
                textView.setText("未认证");
                textView.setTextColor(Color.parseColor("#999999"));
                break;
            case 0: // 认证失败
                imageView.setImageResource(R.drawable.upload_success_ic);
                textView.setText("认证失败");
                textView.setTextColor(Color.parseColor("#E02020"));
                break;
            case 1: // 认证成功
                imageView.setImageResource(R.drawable.upload_success_ic);
                textView.setText("认证成功");
                textView.setTextColor(Color.parseColor("#3265F9"));
                break;
            case -1: // 提交全部认证资料 认证中
                imageView.setImageResource(R.drawable.upload_success_ic);
                textView.setText("认证中");
                textView.setTextColor(Color.parseColor("#ff9900"));
                break;
            default:
                break;
        }
    }

    @SingleClick
    @Override
    public void onClick(View v) {
        if (binding.tvUploadIdCardStatus.equals(v)) {
            mCurrentSelectIndex = 0;
            startActivityForResult(UploadIdCardActivity.class, new OnActivityCallback() {
                @Override
                public void onActivityResult(int resultCode, @Nullable Intent data) {
                    if (RESULT_OK == resultCode) {
                        getIdCardStatus();
                    }
                }
            });
        } else if (binding.tvUploadDrivingLicenseStatus.equals(v)) {
            mCurrentSelectIndex = 1;
            startActivityForResult(UploadDrivingLicenseActivity.class, new OnActivityCallback() {
                @Override
                public void onActivityResult(int resultCode, @Nullable Intent data) {
                    if (RESULT_OK == resultCode) {
                        getDrivingLicenseStatus();
                    }
                }
            });
        } else if (binding.tvUploadQualificationCertificateStatus.equals(v)) {
            mCurrentSelectIndex = 2;
            startActivityForResult(UploadQualificationCertificateActivity.class, new OnActivityCallback() {
                @Override
                public void onActivityResult(int resultCode, @Nullable Intent data) {
                    if (RESULT_OK == resultCode) {
                        getQualificationStatus();
                    }
                }
            });
        } else if (binding.tvUploadVehicleLicenseStatus.equals(v)) {
            mCurrentSelectIndex = 3;
            startActivityForResult(UploadVehicleLicenseActivity.class, new OnActivityCallback() {
                @Override
                public void onActivityResult(int resultCode, @Nullable Intent data) {
                    if (RESULT_OK == resultCode) {
                        getVehicleLicenseStatus();
                    }
                }
            });
        } else if (binding.tvUploadRoadTransportStatus.equals(v)) {
            mCurrentSelectIndex = 4;
            startActivityForResult(UploadRoadTransportActivity.class, new OnActivityCallback() {
                @Override
                public void onActivityResult(int resultCode, @Nullable Intent data) {
                    if (RESULT_OK == resultCode) {
                        getRoadTransportStatus();
                    }
                }
            });
        } else if (binding.tvUploadPolicyOfInsuranceStatus.equals(v)) {
            mCurrentSelectIndex = 5;
            startActivityForResult(UploadPolicyOfInsuranceActivity.class, new OnActivityCallback() {
                @Override
                public void onActivityResult(int resultCode, @Nullable Intent data) {
                    if (RESULT_OK == resultCode) {
                        getInsuranceStatus();
                    }
                }
            });
        } else if (binding.tvLicensePlateNumber.equals(v)) {
            if (null != licensePlateColorDialog) licensePlateColorDialog.show();
            else getLicensePlateColor();
        } else if (binding.tvBtnSubmitCertificate.equals(v)) {
            submitInfo();
        } else {
            mCurrentSelectIndex = -1;
            toast("Oh no something wrong!!!");
        }
    }

    /**
     * 身份验证
     */
    private void submitInfo() {
        if (TextUtils.isEmpty(licensePlateColor)) {
            toast(R.string.plz_selected_license_plate_number);
            return;
        }

        EasyHttp.put(this)
                .api(new SubmitCertificateApi())
                .request(new HttpCallback<HttpData<Void>>(this) {

                    @Override
                    public void onSucceed(HttpData<Void> data) {
                        BusUtils.postSticky(HomeActivity.TAG_REFRESH_USER_DATA); // 刷新用户信息
                        getMyCertificationStatus();
                    }
                });
    }
}
